$.extend($.fn.validatebox.defaults.rules, {
    equalsPwd: {
        validator: function (value, param) {
            return value === $(param[0]).val();
        },
        message: '两次输入的密码不一致 !'
    }
});

$(function () {

    var $tableIns = $("#table-users"),
        $usersForm = $("#users-form"),
        $dialogUserEdit = $("#dialog-user-edit"),
        $dialogUserRoleTree = $("#dialog-user-role-tree");

    // 加载接口列表
    $tableIns.datagrid({
        url: _ctx + "/sysUser/list",
        idField: "id",
        rownumbers: true,
        border: false,
        toolbar: "#toolbar",
        checkOnSelect: false,
        selectOnCheck: false,
        fitColumns: true,
        singleSelect: true,
        fit: true,
        frozenColumns: [[
            {field: '', title: '', checkbox: true},
            {field: 'id', title: '标识', width: 280}
        ]],
        columns: [[
            {field: 'uname', title: '用户名', width: 350},
            {field: 'pword', title: '密码', width: 250, hidden: true},
            {field: 'rname', title: '姓名', width: 250},
            {field: 'createDate', title: '创建时间', width: 250}
        ]]
    });

    $("#add").click(function () {
        $dialogUserEdit.dialog({
            title: "新增",
            iconCls: "icon-add",
            buttons: [{
                text: "确定",
                iconCls: "icon-ok",
                handler: function () {
                    var validateResult = $usersForm.form("validate");
                    if (validateResult) {
                        $.post(_ctx + "/sysUser/insert", $usersForm.serializeArray(), function (result) {
                            if (result.success) {
                                $dialogUserEdit.dialog("close");
                                $.messager.alert("提示", result.message, "info", function () {
                                    $tableIns.datagrid("reload");
                                });
                            } else {
                                $.messager.alert("提示", result.message, "info");
                            }
                        }, "json");
                    }
                }
            }, {
                text: "取消",
                iconCls: "icon-cancel",
                handler: function () {
                    $dialogUserEdit.dialog("close");
                }
            }]
        });
        $usersForm.form("clear");
        $dialogUserEdit.dialog("open");
    });

    $("#update").click(function () {
        var dataRows = $tableIns.datagrid("getSelections");
        if (dataRows.length === 1) {
            $dialogUserEdit.dialog({
                title: "修改",
                iconCls: "icon-edit",
                buttons: [{
                    text: "确定",
                    iconCls: "icon-ok",
                    handler: function () {
                        var validateResult = $usersForm.form("validate");
                        if (validateResult) {
                            $.post(_ctx + "/sysUser/update", $usersForm.serializeArray(), function (result) {
                                if (result.success) {
                                    $dialogUserEdit.dialog("close");
                                    $.messager.alert("提示", result.message, "info", function () {
                                        $tableIns.datagrid("reload");
                                    });
                                } else {
                                    $.messager.alert("提示", result.message, "info");
                                }
                            }, "json");
                        }
                    }
                }, {
                    text: "取消",
                    iconCls: "icon-cancel",
                    handler: function () {
                        $dialogUserEdit.dialog("close");
                    }
                }]
            });
            $usersForm.form("load", dataRows[0]);
            $dialogUserEdit.dialog("open");
        }
    });

    $("#delete").click(function () {
        var dataRows = $tableIns.datagrid("getChecked");
        if (dataRows.length > 0) {
            $.messager.confirm("提示", "确定要删除选中的" + dataRows.length + "行 ?", function (r) {
                if (r) {
                    var idArr = [];
                    for (var i = 0; i < dataRows.length; i++) {
                        idArr.push(dataRows[i].id);
                    }
                    $.post(_ctx + "/sysUser/delete", {ids: idArr.join(",")}, function (result) {
                        if (result.success) {
                            $dialogUserEdit.dialog("close");
                            $.messager.alert("提示", result.message, "info", function () {
                                $tableIns.datagrid("reload");
                                $tableIns.datagrid("uncheckAll");
                            });
                        } else {
                            $.messager.alert("提示", result.message, "info");
                        }
                    }, "json");
                }
            });
        }
    });

    $("#userSet").click(function () {
        var dataRow = $tableIns.datagrid("getSelected");
        if (dataRow) {
            $dialogUserRoleTree.dialog({
                title: "[<font color='blue'>" + dataRow.rname + "</font>]的角色设置"
            });

            $dialogUserRoleTree.dialog("open");

            $.post(_ctx + "/sysRole/roleListByUserId", {
                userId: dataRow.id
            }, function (result) {
                if (result.success) {
                    var setting = {
                        data: {
                            key: {
                                name: 'rolename'
                            },
                            simpleData: {
                                enable: true,
                                idKey: 'id',
                                pIdKey: 'parentId'
                            }
                        },
                        check: {
                            enable: true,
                            chkStyle: "checkbox",
                            chkboxType: {"Y": "ps", "N": "ps"}
                        },
                        callback: {
                            onCheck: function (event, treeId, treeNode) {
                                $.post(_ctx + "/sysUserRole/updateRoleAuth", {
                                    roleId: treeNode.id,
                                    userId: dataRow.id,
                                    opera: treeNode.checked
                                }, function (result) {
                                    if (!result.success) {
                                        $.messager.alert("提示", result.message, "warning");
                                    }
                                }, "json");
                            }
                        }
                    };
                    $.fn.zTree.init($("#role-tree"), setting, result.data);
                } else {
                    $.messager.alert("提示", result.message, "warning");
                }
            }, "json");
        }
    });
});